//
//  YearView.m
//  carcareIOS
//
//  Created by 王然 on 16/4/22.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "YearView.h"
#import "FuelMgr.h"
#define VIEW_HEIGHT_ONE (48)

typedef NS_ENUM(NSInteger , View_Type) {
    View_Type_L = 10,//label
    View_Type_B = 20,//Button
    View_Type_ImageV = 30,//selectImage
    
};

@interface YearView()

@property (nonatomic , strong)UIView * backImageV;

@property (nonatomic , copy) void (^btnClick)(NSInteger index);

@property (nonatomic , assign) CGFloat bottomY;

@end

@implementation YearView

- (instancetype)initOnView:(UIView *)view
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.layer.cornerRadius = 4;
        self.layer.masksToBounds = YES;
        self.bottomY = 2;
        
        self.backImageV = [[[UIView viewWithFrame:CGRectZero] resetBackgroundColor:[UIColor clearColor]] setupOnView:self];
        [view addSubview:self];

    }
    return self;
}
- (void)drawRect:(CGRect)rect
{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShadowWithColor(context, CGSizeMake(0, 1), 1, [[UIColor blackColor] colorWithAlphaComponent:0.6].CGColor);
    CGFloat triangleW = 15;
    CGFloat triangleH = 10;
    CGFloat triangleRLineW = 25;
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextMoveToPoint(context, VIEW_WIDTH - self.bottomY - triangleRLineW - triangleW, triangleH);
    CGFloat smallLWAndH = 3;
    CGContextAddLineToPoint(context, VIEW_WIDTH - self.bottomY - triangleRLineW - triangleW / 2 - smallLWAndH / 2, smallLWAndH);

    CGContextAddArcToPoint( context, VIEW_WIDTH - self.bottomY - triangleRLineW - triangleW / 2 + smallLWAndH / 2, smallLWAndH, smallLWAndH, smallLWAndH, 3);
    CGContextAddLineToPoint(context, VIEW_WIDTH - self.bottomY - triangleRLineW , triangleH);
    CGContextDrawPath(context, kCGPathFill);
    
    CGFloat lineW = VIEW_WIDTH - self.bottomY * 2;
    CGFloat lineH =  VIEW_HEIGHT_ONE * [FuelMgr sharedInstance].years.count;
    CGContextMoveToPoint(context, self.bottomY + lineW / 2, triangleH);
    CGContextAddArcToPoint(context, self.bottomY + lineW, triangleH, self.bottomY + lineW, triangleH + lineH / 2, 4);
    CGContextAddArcToPoint(context, self.bottomY + lineW, triangleH + lineH, self.bottomY + lineW / 2, triangleH + lineH , 4);
    CGContextAddArcToPoint(context, self.bottomY , triangleH + lineH, self.bottomY, triangleH + lineH / 2, 4);
    CGContextAddArcToPoint(context, self.bottomY, triangleH, self.bottomY + lineW / 2, triangleH, 4);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextDrawPath(context, kCGPathFill);
    
    CGContextClosePath(context);
    
    
}
- (void)loadDataWithFrameX:(CGFloat)frameX withFrameY:(CGFloat)frameY  withArr:(NSArray *)dataArr withClick:(void(^)(NSInteger index))selectClick
{
    if(selectClick) self.btnClick = selectClick;
    
    CGFloat topY = 10;
    CGFloat btnH = VIEW_HEIGHT_ONE;
    
    [self setFrame:CGRectMake(frameX, frameY, VIEW_WIDTH, topY + self.bottomY * 2 + btnH * dataArr.count)];
    [self.backImageV setFrame:CGRectMake(0, 0, VIEW_WIDTH, self.height)];

    [self.backImageV removeAllSubviews];
    
    for (int index = 0; index < dataArr.count ; index ++) {
        CGFloat btnX = 5;
        CGFloat btnW = VIEW_WIDTH - btnX * 2;
        
        CGFloat btnY = topY + btnH * index;
        UIImage * selectImage = ImageNamed(@"yearSelectImage");
        UIImageView * selectImageV = [Factory imageViewWithCenter:CGPointMake(btnX + btnW - selectImage.size.width / 2 * 3 , btnY + btnH / 2) image:selectImage onView:self.backImageV];
        selectImageV.tag = View_Type_ImageV + index;
        selectImageV.hidden = YES;
        
        UILabel * titleLabel = [Factory labelWithFrame:CGRectMake(btnX, btnY, btnW - selectImage.size.width * 2, btnH) font:FONT_DEFAULT_Light(14) text:[NSString stringWithFormat:@"%@",dataArr[index]] textColor:[Theme wordGrayColor] onView:self.backImageV textAlignment:NSTextAlignmentCenter];
        titleLabel.tag = View_Type_L + index;
        UIButton * btn = [[UIButton buttonWithFrame:CGRectMake(btnX, btnY, btnW, btnH) title:@"" textColor:[Theme wordGrayColor] font:FONT_BUTTON click:nil] setupOnView:self.backImageV];
        
        btn.tag = View_Type_B + index;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        if ([FuelMgr sharedInstance].choiceYear == [dataArr[index] integerValue]) {
            [titleLabel setTextColor:[Theme wordColor]];
            [selectImageV setHidden:NO];
        }
        
        if(index != dataArr.count - 1)[[[UIView viewWithFrame:CGRectMake(btnX, btn.bottom - 0.5, btnW, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:self.backImageV];
    }
    [self setNeedsDisplay];
}
- (void)btnClick:(UIButton *)sender
{
    NSInteger selectIndex = sender.tag - View_Type_B;
    for (int index = 0; index < [FuelMgr sharedInstance].years.count; index ++)
    {
        UILabel * label = (UILabel *)[self.backImageV viewWithTag:View_Type_L + index];
        UIImageView * imageV = (UIImageView *)[self.backImageV viewWithTag:View_Type_ImageV + index];
        [label setTextColor:(selectIndex == index ? [Theme wordColor] :[Theme wordGrayColor])];
        [imageV setHidden:!(selectIndex == index)];
    }
    if(self.btnClick)self.btnClick(selectIndex);
}
@end
